package com.hnkywl.sts.service.zjgl;

import com.hnkywl.sts.dao.htgl.CghtDao;
import com.hnkywl.sts.dao.sfgl.SkshDao;
import com.hnkywl.sts.dao.zjgl.HthkDao;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.zjgl.Hthk;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.CalcUtils;
import com.hnkywl.sts.util.DateUtil;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.service.sfgl
 * 类描述:   付款审核服务类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class HthkService {
    private HthkDao hthkDao;
    private SkshDao skshDao;
    private CghtDao cghtDao;

    @Transactional
    public String findAllHthk(Long gsId, String khMc, String hth, int page, int rows) {
        Page<Hthk> hthkPage = this.hthkDao.findBzjList(gsId, khMc, hth, page, rows);
        List<Hthk> hthkList = hthkPage.getResult() == null ? new ArrayList<Hthk>() : hthkPage.getResult();
        String bzjJson = BaseUtils.fetchCollectionToJson(hthkList, "id", "nbgs.jc", "cght.htbm", "cght.hsze", "kh.qc", "fklx", "khfksj", "sk.bm", "je", "ye", "txl", "endDate", "tx", "txye", "mxts", "jsts", "zt");
        return "{\"total\":" + hthkPage.getTotalCount() + ",\"rows\":[" + bzjJson + "]}";
    }

    @Transactional
    public void saveHthk(Hthk hthk) {
        hthk.setDateCreated(new Date());
        hthk.setCreateUser(YgService.getCurrentYg());
        hthk.setCreateOrgan(YgService.getCurrentYg().getZggs());
        hthk.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        hthk.setLastUpdated(hthk.getDateCreated());
        Sfk sk = this.skshDao.get(hthk.getSk().getId());
        sk.setZt("ysd");
        hthk.setJe(sk.getYe());
        hthk.setZt("2");
        hthk.setKhfksj(sk.getKhfksj());
        hthk.setFklx(sk.getFkfs().getZ().equals("承兑") ? "1" : "0");
        hthk.setKxsy(sk.getKxsy());
        hthk.setFkfs(sk.getFkfs());
        if (sk.getFkfs().getZ().equals("承兑")) {
            if (sk.getSfjstx().equals("0")) {
                Cght cght = this.cghtDao.get(hthk.getCght().getId());
                long calDays;
                long reduceDays = DateUtil.getDay(sk.getEndDate(), sk.getKhfksj());
                calDays = reduceDays - cght.getMxts();
                Double tx = 0.0;
                if (calDays > 0) {
                    tx = CalcUtils.round(((sk.getYe() * cght.getTxl() / 100) / 30.0) * calDays, 2);
                }
                hthk.setTx(tx);
                hthk.setTxye(tx);
                hthk.setMxts(cght.getMxts());
                hthk.setEndDate(sk.getEndDate());
                hthk.setJsts(calDays);
                hthk.setTxl(CalcUtils.round(cght.getTxl(), 4));
                hthk.setYe(CalcUtils.round((sk.getYe() - tx),4));
              //  hthk.setYe(sk.getYe() - tx);
                sk.setSfjstx("1");
            } else {
                hthk.setTx(sk.getTx());
                hthk.setYe(sk.getYe());
            }
        } else {
            hthk.setYe(sk.getYe());
        }
        this.skshDao.save(sk);
        this.hthkDao.save(hthk);
    }


    @Transactional
    public void jsHthk(Long hthkid) {
        Hthk hthk = this.hthkDao.get(hthkid);
        hthk.setZt("1");
        this.hthkDao.save(hthk);
        Sfk sfk = this.skshDao.get(hthk.getSk().getId());
        sfk.setZt("ytz");
        if (hthk.getFkfs().getZ() == "承兑") {
            sfk.setTx(CalcUtils.round(hthk.getTx(), 2));
            sfk.setTxl(hthk.getTxl());
            sfk.setMxts(hthk.getMxts());
            sfk.setJstx(hthk.getJsts());
            sfk.setTxye(hthk.getTxye());
        }
        sfk.setYe(CalcUtils.round(hthk.getYe(), 2));
        this.skshDao.save(sfk);
    }
    @Transactional
    public void sdHthk(Long hthkid) {
        Hthk hthk = this.hthkDao.get(hthkid);
//        Sfk sk = this.skshDao.get(hthk.getSk().getId());
//
//        if (sk.getFkfs().getZ().equals("承兑")) {
//            Cght cght = this.cghtDao.get(hthk.getCght().getId());
//            long calDays;
//            long reduceDays = DateUtil.getDay(sk.getEndDate(), sk.getKhfksj());
//            calDays = reduceDays - cght.getMxts();
//            Double tx = 0.0;
//            if (calDays > 0) {
//                tx = CalcUtils.round(((sk.getYe() * cght.getTxl() / 100) / 30.0) * calDays, 2);
//            }
//            sk.setTx(tx);
//            sk.setYe(sk.getYe() - tx);
//            sk.setMxts(cght.getMxts());
//            sk.setJstx(calDays);
//            sk.setZt("ysd");
//            sk.setLastUpdated(new Date());
//            skshDao.save(sk);
//            hthk.setZt("0");
//            this.hthkDao.save(hthk);
//        }
//        else{
//            sk.setZt("ysd");
//            sk.setLastUpdated(new Date());
//            skshDao.save(sk);
            hthk.setZt("0");
            this.hthkDao.save(hthk);
//        }

    }
    @Transactional
    public void deletehthk(Long id) {
        Hthk hthk = hthkDao.get(id);
        hthk.setLastUpdated(new Date());
        hthk.setDeleted(1);
        this.hthkDao.save(hthk);
        Sfk sfk = this.skshDao.get(hthk.getSk().getId());
        sfk.setZt("ytz");
        this.skshDao.save(sfk);
    }
    @Transactional
    public Hthk findHthkByCghtId(Long CghtId) {
        return hthkDao.findHthkByCghtId(CghtId);
    }

    public List<Hthk> findHthkByCght(Cght cght) {
        return this.hthkDao.findByProperty("cght", cght, null);
    }

    @Autowired
    public void setHthkDao(HthkDao hthkDao) {
        this.hthkDao = hthkDao;
    }

    @Autowired
    public void setSkshDao(SkshDao skshDao) {
        this.skshDao = skshDao;
    }

    @Autowired
    public void setCghtDao(CghtDao cghtDao) {
        this.cghtDao = cghtDao;
    }


}
